// https://leetcode.cn/problems/find-all-anagrams-in-a-string/

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> rec(256, 0);
        vector<int> res;
        int cnt = 0;
        int j = 0;
        for (int i = 0; i < p.length(); ++i) {
            rec[p[i]]++;
        }
        for (int i = 0; i < s.length(); ++i) {
            rec[s[i]]--;
            if (rec[s[i]] >= 0) {
                cnt++;
                if (cnt == p.length()) res.push_back(j);
            }
            if (i - j >= p.length() - 1) {
                rec[s[j]]++;
                if (rec[s[j]] > 0) cnt--; 
                j++; 
            }
        }
        return res;
    }
};